/*
 *  (C) Copyright 2008 Insula Tecnologia da Informacao Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.dao.hibernate;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.ReplicationMode;
import org.hibernate.Session;
import org.openinsula.vulcano.orm.dao.query.DaoQuery;
import org.openinsula.vulcano.orm.dao.query.hibernate.HibernateDaoQueryTranslator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

/**
 * @author Eduardo R Danielli
 * 
 */
@Repository
@SuppressWarnings("unchecked")
public class HibernateDaoImpl extends HibernateDaoSupport implements
		HibernateDao {

	@Autowired
	private HibernateDaoQueryTranslator daoQueryTranslator;

	@Override
	public void clear() {
		getHibernateTemplate().clear();
	}

	@Override
	public void delete(final Object entity) {
		getHibernateTemplate().delete(entity);
	}

	@Override
	public void deleteAll(final Collection<?> entities) {
		getHibernateTemplate().deleteAll(entities);
	}

	@Override
	public int deleteAll(final Class<?> entityClass) {
		return getHibernateTemplate().bulkUpdate("delete " + entityClass.getName());
	}
	
	@Override
	public <T> List<T> find(final String queryString, final Object... values) {
		return getHibernateTemplate().find(queryString, values);
	}

	@Override
	public <T> T findById(final Class<T> entityClass, final Object id) {
		return (T) getHibernateTemplate().get(entityClass, (Serializable) id);
	}

	@Override
	public void flush() {
		getHibernateTemplate().flush();
	}

	@Override
	public <T> List<T> listAll(final Class<T> type) {
		return getHibernateTemplate().loadAll(type);
	}

	@Override
	public void reattach(final Object entity) {
		getHibernateTemplate().lock(entity, LockMode.NONE);
	}

	@Override
	public void refresh(final Object entity) {
		getHibernateTemplate().refresh(entity);
	}

	@Override
	public void replicate(final Object entity) {
		getHibernateTemplate().replicate(entity, ReplicationMode.OVERWRITE);
	}

	@Override
	public void save(final Object entity) {
		getHibernateTemplate().saveOrUpdate(entity);
	}

	@Override
	public void saveAll(final Collection<?> entities) {
		getHibernateTemplate().saveOrUpdateAll(entities);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T merge(final T entity) {
		return (T) getHibernateTemplate().merge(entity);
	}
	
	@Override
	public <T> T findUnique(final String queryString, final Object... values) {
		List<?> result = find(queryString, values);

		if (result.size() != 1) {
			throw new IllegalStateException("Query did not return unique result: " + result.size());
		}

		return (T) result.get(0);
	}

	@Override
	public int update(final String queryString, final Object... values) {
		return getHibernateTemplate().bulkUpdate(queryString, values);
	}

	@Override
	public <T> List<T> find(final DaoQuery daoQuery) {
		Session session = getSession(false);

		try {
			Query query = session.createQuery(daoQueryTranslator.createHql(daoQuery));
			daoQueryTranslator.configureQuery(query, daoQuery);

			return query.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
	}

	@Override
	public <T> T findUnique(final DaoQuery daoQuery) {
		List queryList = find(daoQuery);

		int resultCount = queryList.size();
		
		if (resultCount > 1) {
			throw new IllegalStateException("Query did not return unique result.");
		}
		
		return (T) (resultCount == 0 ? null : queryList.get(0));
	}

	@Override
	public void evict(final Object entity) {
		getHibernateTemplate().evict(entity);
	}

}
